{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "19076b00",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/bjn_Latn.dev\n",
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/eng_Latn.dev\n",
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/ind_Latn.dev\n",
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/jav_Latn.dev\n",
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/zsm_Latn.dev\n",
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/zho_Hans.dev\n",
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/tam_Taml.dev"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "52007167",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('ind_Latn.dev') as fopen:\n",
    "    ind = fopen.read().split('\\n')\n",
    "    \n",
    "with open('zsm_Latn.dev') as fopen:\n",
    "    ms = fopen.read().split('\\n')\n",
    "    \n",
    "ind_, ms_ = [], []\n",
    "for i in range(len(ind)):\n",
    "    if len(ind[i]) and len(ms[i]):\n",
    "        ind_.append(ind[i])\n",
    "        ms_.append(ms[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7eb3ed13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(997, 997)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(ind_), len(ms_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8c160b3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "import requests\n",
    "import os\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8a222587",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mkdir: cannot create directory ‘google-translate-ind-ms’: File exists\r\n"
     ]
    }
   ],
   "source": [
    "!mkdir google-translate-ind-ms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0b246de3",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████████| 997/997 [52:42<00:00,  3.17s/it]\n"
     ]
    }
   ],
   "source": [
    "for i in tqdm(range(len(ms_))):\n",
    "    filename = os.path.join('google-translate-ind-ms', f'{i}.json')\n",
    "    \n",
    "    if os.path.exists(filename):\n",
    "        continue\n",
    "    \n",
    "    headers = {\n",
    "        'accept': 'application/json',\n",
    "        'Content-Type': 'application/json',\n",
    "    }\n",
    "    \n",
    "    l = ind_[i]\n",
    "\n",
    "    json_data = {\n",
    "        'text': l,\n",
    "    }\n",
    "\n",
    "    response = requests.post('http://localhost:8005/translate', headers=headers, json=json_data)\n",
    "    r = response.json()\n",
    "    with open(filename, 'w') as fopen:\n",
    "        json.dump({'text': l, 'r': r}, fopen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "48ee2392",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sacrebleu.metrics import BLEU, CHRF, TER\n",
    "\n",
    "chrf = CHRF(word_order = 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "851b7c47",
   "metadata": {},
   "outputs": [],
   "source": [
    "predicted, actual = [], []\n",
    "for i in range(len(ms_)):\n",
    "    try:\n",
    "        filename = os.path.join('google-translate-ind-ms', f'{i}.json')\n",
    "        with open(filename) as fopen:\n",
    "            d = json.load(fopen)\n",
    "            \n",
    "        n = d['r']['result']\n",
    "        predicted.append(d['r']['result'])\n",
    "        actual.append(ms_[i])\n",
    "    except:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "eea57aec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "chrF2++ = 57.54"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score = chrf.corpus_score(predicted, [actual])\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "298f3bf5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
